const {queryMysql} = require('../utils/queryMysqlUtil')
const dayjs = require("dayjs");

exports.getAllFilterRulerList = async function () {
    const queryStr = `select filter_ruler,branch_id,group_id,chihiro_filter_ruler.create_time,chihiro_filter_ruler.update_time from chihiro_filter_ruler right join chihiro_team_group on chihiro_filter_ruler.group_id=chihiro_team_group.id`
    let {results} = await queryMysql(queryStr).catch((err) => {
        return Promise.reject(err)
    })
    return results
}

//根据仓库id查到其适用的组过滤规则
exports.getFilterRulerByRepoId=async function (repoId){
    const queryStr = `
        SELECT * FROM chihiro_filter_ruler 
        WHERE group_id in 
        (SELECT chihiro_team_group.id FROM chihiro_gitlab_detail LEFT JOIN chihiro_team_group ON chihiro_gitlab_detail.group_id=chihiro_team_group.id WHERE chihiro_gitlab_detail.id=${repoId})`
    let {results} = await queryMysql(queryStr).catch((err) => {
        return Promise.reject(err)
    })
    return results
}

exports.getFilterRulerListByGroupId = async function (groupId) {
    const queryStr = `select chihiro_filter_ruler.id as filterId,filter_ruler,branch_id,group_id,chihiro_filter_ruler.create_time,chihiro_filter_ruler.update_time from chihiro_filter_ruler left join chihiro_team_group on chihiro_filter_ruler.group_id=chihiro_team_group.id where chihiro_team_group.id=${groupId}`
    let {results} = await queryMysql(queryStr).catch((err) => {
        return Promise.reject(err)
    })
    return results
}


exports.isExistsFilterRulerInSpecGroup = async function (groupId, filterRuler) {
    const queryStr = `select count(*) as count from chihiro_filter_ruler where '${filterRuler}'  in (select filter_ruler from chihiro_filter_ruler where group_id=${groupId})`
    let {results} = await queryMysql(queryStr).catch((err) => {
        return Promise.reject(err)
    })
    return results
}


exports.modifyFilterRuler = async function ({rid, groupId,filterRuler}) {
    const now = dayjs().format("YYYY-MM-DD HH:mm:ss");
    const queryStr = `update chihiro_filter_ruler set group_id=${groupId},filter_ruler='${filterRuler}',create_time='${now}' where id='${rid}'`
    let {results} = await queryMysql(queryStr).catch((err) => {
        return Promise.reject(err)
    })
    return results
}

exports.addFilterRuler = async function (reqData) {
    const now = dayjs().format("YYYY-MM-DD HH:mm:ss");
    const {groupId,filterRuler}=reqData
    const queryStr = `insert into chihiro_filter_ruler(group_id,filter_ruler,create_time,update_time) values('${groupId}','${filterRuler}','${now}','${now}')`
    let {results} = await queryMysql(queryStr).catch((err) => {
        return Promise.reject(err)
    })
    return results
}

exports.deleteFilterRulerById = async function (filterRulerId) {
    const queryStr = `delete from chihiro_filter_ruler where id=${filterRulerId}`
    let {results} = await queryMysql(queryStr).catch((err) => {
        return Promise.reject(err)
    })
    return results
}
